package com.jxb.mybatis;

import com.jxb.DO.User;
import com.jxb.mybatis.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class App {

    public static void main(String[] args) throws IOException {
        String resource = "mybatis.xml";
        //将xml配置文件构建成Configuration配置类
        Reader reader = Resources.getResourceAsReader(resource);
        //通过加载配置文件流构建一个SqlSessionFactory
        SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
        //获取数据源，执行器
        SqlSession sqlSession = sqlMapper.openSession();
        try {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            System.out.println(mapper.selectById(1));
            User user = mapper.selectById(1);
            sqlSession.clearCache();
            System.out.println(user.getName());
        }catch (Exception e) {
            System.out.println(e);
        }finally {
            sqlSession.close();
        }
    }

}
